package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.NewsCommend;
import cn.wolfcode.trip.base.query.NewsCommendQuery;
import cn.wolfcode.trip.base.service.INewsCommendService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
/**
 * 日报推荐资源后台控制器
 */
@Controller
@RequestMapping("newsCommend")
public class NewsCommendController {

    @Autowired
    private INewsCommendService newsCommendService;

    /**
     * 列表界面
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") NewsCommendQuery qo,Model model){
        qo.setOrderBy("schedule desc");
        model.addAttribute("pageInfo",newsCommendService.query(qo));
        return "newsCommend/list";
    }

    /**
     * 新增/修改
     * @param commend
     * @param file
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(NewsCommend commend, MultipartFile file){
        //判断是否有上传图片
        if(file!=null&&file.getSize()>0){
            //保存图片
            String url = UploadUtil.uploadQiniu(file);
            //把图片地址设置到coverUrl属性
            commend.setCoverUrl(url);
        }
        newsCommendService.saveOrUpdate(commend);
        return new JsonResult();
    }
}
